package com.lonely.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lonely.common.core.domain.Select;
import com.lonely.common.core.domain.api.R;
import com.lonely.common.utils.SecurityUtils;
import com.lonely.system.domain.dto.role.BindUserRoleDto;
import com.lonely.system.domain.dto.user.BindUserDataPermissionsDto;
import com.lonely.system.domain.dto.user.SysUserPageQueryDto;
import com.lonely.system.domain.dto.user.SysUserSubmitDto;
import com.lonely.system.domain.dto.user.UpdateUserPasswordDTO;
import com.lonely.system.domain.vo.system.user.SysUserDetailVo;
import com.lonely.system.domain.vo.system.user.SysUserPageQueryVo;
import com.lonely.system.service.ISysDataPermissionsService;
import com.lonely.system.service.ISysUserRoleService;
import com.lonely.system.service.ISysUserService;
import com.lonely.system.utils.AuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 *
 * @author fzzf
 */
@RestController
@RequestMapping("/systemManage/user")
@Api(tags = "系统管理/用户管理")
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Autowired
    private ISysDataPermissionsService sysDataPermissionsService;

    /**
     * 获取用户分页列表
     */
    @PostMapping("/page")
    @ApiOperation("获取用户分页列表")
    public R<Page<SysUserPageQueryVo>> page(@RequestBody SysUserPageQueryDto queryDto) {
        return R.data(userService.page(queryDto));
    }

    /**
     * 创建用户信息
     *
     * @param submitDto
     * @return
     */
    @SaCheckPermission("system:user:create")
    @PostMapping("/create")
    @ApiOperation("创建用户信息")
    public R<Boolean> create(@RequestBody @Validated SysUserSubmitDto submitDto) {
        this.userService.create(submitDto);
        return R.status(true);
    }


    /**
     * 更新用户信息
     *
     * @param submitDto
     * @return
     */
    @SaCheckPermission("system:user:edit")
    @PostMapping("/edit")
    @ApiOperation("更新用户信息")
    public R<Boolean> edit(@RequestBody @Validated SysUserSubmitDto submitDto) {
        this.userService.edit(submitDto);
        return R.status(true);
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/detail/{userId}")
    @ApiOperation("获取用户信息")
    public R<SysUserDetailVo> detail(@PathVariable("userId") Long userId) {
        return R.data(this.userService.detail(userId));
    }


    /**
     * 绑定用户角色关联
     *
     * @param bindUserRoleDto
     * @return
     */
    @SaCheckPermission("system:user:bindUserRoles")
    @PostMapping("/bindUserRoles")
    @ApiOperation(value = "绑定用户角色关联")
    public R<Boolean> bindUserRoles(@RequestBody @Validated BindUserRoleDto bindUserRoleDto) {
        this.sysUserRoleService.bindUserRoles(bindUserRoleDto);
        // 判断当前操作用户是否是登录用户
        AuthUtil.refreshUserPermission(bindUserRoleDto.getUserId());
        return R.status(true);
    }


    /**
     * 解除用户角色关联
     *
     * @param unBindUserRoleDto
     * @return
     */
    @SaCheckPermission("system:user:unBindUserRoles")
    @PostMapping("/unBindUserRoles")
    @ApiOperation(value = "解除用户角色关联")
    public R<Boolean> unBindUserRoles(@RequestBody @Validated BindUserRoleDto unBindUserRoleDto) {
        this.sysUserRoleService.unBindUserRoles(unBindUserRoleDto);

        // 判断当前操作用户是否是登录用户
        AuthUtil.refreshUserPermission(unBindUserRoleDto.getUserId());
        return R.status(true);
    }


    /**
     * 绑定用户数据权限关联
     *
     * @param bindUserDataPermissionsDto
     * @return
     */
    @SaCheckPermission("system:user:bindUserPermissions")
    @PostMapping("/bindUserPermissions")
    @ApiOperation(value = "绑定用户数据权限关联")
    public R<Boolean> bindUserPermissions(@RequestBody @Validated BindUserDataPermissionsDto bindUserDataPermissionsDto) {
        this.sysDataPermissionsService.bindUserPermissions(bindUserDataPermissionsDto);

        // 更新用户权限
        AuthUtil.refreshUserPermission(SecurityUtils.getUserId());
        return R.status(true);
    }

    /**
     * 显示指定用户的手机号码
     *
     * @param userId
     * @return
     */
    @GetMapping("/getDeclarePhone/{userId}")
    @ApiOperation("显示指定用户的手机号码")
    public R<String> getDeclarePhone(@PathVariable("userId") Long userId) {
        return R.data(this.userService.getDeclarePhone(userId));
    }

    /**
     * 离职
     *
     * @param userId
     * @return
     */
    @GetMapping("/resign/{userId}")
    @ApiOperation("离职")
    @SaCheckPermission("system:user:resign")
    public R<Boolean> resign(@PathVariable("userId") Long userId) {
        this.userService.resign(userId);
        return R.status(true);
    }


    /**
     * 启用
     *
     * @param userId
     * @return
     */
    @GetMapping("/enabled/{userId}")
    @ApiOperation("启用")
    @SaCheckPermission("system:user:enabled")
    public R<Boolean> enabled(@PathVariable("userId") Long userId) {
        this.userService.enabled(userId);
        return R.status(true);
    }


    /**
     * 停用
     *
     * @param userId
     * @return
     */
    @GetMapping("/disabled/{userId}")
    @ApiOperation("停用")
    public R<Boolean> disabled(@PathVariable("userId") Long userId) {
        this.userService.disabled(userId);
        return R.status(true);
    }


    /**
     * 获取当前登录用户对应产线的跟单员列表
     *
     * @return
     */
    @GetMapping("/getMerchandiserSelectOptions")
    public R<List<Select>> getMerchandiserSelectOptions() {
        return R.data(this.userService.getMerchandiserSelectOptions());
    }


    /**
     * 获取当前登录用户对应产线的设计师列表
     *
     * @return
     */
    @GetMapping("/getDesignerSelectOptions")
    public R<List<Select>> getDesignerSelectOptions() {
        return R.data(this.userService.getDesignerSelectOptions());
    }

    /**
     * 获取当前登录用户对应有权限的生产工种为裁剪的列表
     *
     * @return
     */
    @GetMapping("/getCuttingBedEmployeeSelectOptions")
    public R<List<Select>> getCuttingBedEmployeeSelectOptions() {
        return R.data(this.userService.getCuttingBedEmployeeSelectOptions());
    }

    /**
     * 修改密码
     *
     * @param requestDto
     * @return
     */
    @PostMapping("/updatePassword")
    @SaCheckPermission("system:user:updatePassword")
    public R<Boolean> updatePassword(@RequestBody @Validated UpdateUserPasswordDTO requestDto) {
        this.userService.updatePassword(requestDto);
        return R.status(true);
    }

}
